package com.zxj.geekstudy.config;


import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.ResourceUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

@Configuration
public class ShardingDataSourceConfig {

    @Primary
    @Bean(name = "shardingDataSource")
    public DataSource getDataSource() throws IOException, SQLException {
        String fileName = ResourceUtils.getURL("classpath:").getPath() + "/sharding-application.yml";
        File yamlFile = new File(fileName);
        DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);
        return dataSource;
    }
}
